<?php
include_once 'controller/default.php';
include_once 'connection/connection_database.php';
include_once 'checkForm/xulychuoi.php';

class categories {

    function Insert($category) {
        $str = trim(khongdau($category["catename"]));
        $link = strtolower(preg_replace('/[^a-z0-9]+/i','-',$str)); 
        
        $fillAll = "SELECT * FROM category WHERE category_url LIKE '%{$link}%'";
        $rs = mysql_query($fillAll);
        if(($rows = mysql_num_rows($rs)) > 0){
            $link .= "-".$rows;
        }
        
        $strQuery = "SELECT category_order FROM category ORDER BY category_order DESC LIMIT 1";
        $rs = mysql_query($strQuery) or die(mysql_error());
        $order = mysql_fetch_array($rs);
        if(isset($order["category_order"])){
            $order["category_order"]++;
        }
        $sql = "INSERT INTO category(category_name, category_order, category_url) VALUES('{$category["catename"]}', '{$order["category_order"]}', '{$link}')";
        $result = mysql_query($sql) or die(mysql_error());
        return $result;
    }

    function Delete($category) {
        $sql = "DELETE FROM category WHERE category_id='{$category["id"]}'";
        $result = mysql_query($sql) or die(mysql_error());
        return $result;
    }
    
    function Update($category) {
        $str = trim(khongdau($category["catename"]));
        $link = strtolower(preg_replace('/[^a-z0-9]+/i','-',$str)); 
        $fillAll = "SELECT * FROM category WHERE category_url LIKE '%{$link}%' and category_id<>'{$category["id"]}'";
        $rs = mysql_query($fillAll);
        if(($rows = mysql_num_rows($rs)) > 0){
            $link .= "-".$rows;
        }
        $sql = "UPDATE category SET category_name='{$category["catename"]}', category_url='{$link}' WHERE category_id='{$category["id"]}'";
        return mysql_query($sql) or die(mysql_error());
    }

    function UpdateOrder($category) {
        $boolean = false;
        foreach ($category["cate_name"] as $_category) {
			$xx = json_decode($_category, true);
            //$categories = json_decode(json_decode('"' . $_category . '"'), true);
            $categories = json_decode($_category, true);
            $sql = "UPDATE category SET category_order='{$categories["order"]}' WHERE category_id='{$categories["cate_id"]}'";
            
            $result = mysql_query($sql) or die(mysql_error());
            if($result){
                $boolean = true;
            } else {
                return false;
            }
        }
        return $boolean;
    }

    function FillAll() {
        $sql = "SELECT * FROM category WHERE category_status = 1 ORDER BY category_order ASC";
        $result = mysql_query($sql) or die(mysql_error());
        return $result;
    }

    function FillOne($category) {
        $sql = "SELECT * FROM category WHERE category_id='{$category["id"]}'";
        $result = mysql_query($sql) or die(mysql_error());
        return mysql_fetch_array($result);
    }

}

?>
